package com.his.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.his.common.PageParam;
import com.his.entity.Bed;
import com.his.entity.Ward;
import com.his.mapper.WardMapper;
import com.his.service.BedService;
import com.his.service.WardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * (Ward)表服务实现类
 *
 * @author makejava
 * @since 2020-10-23 18:15:12
 */
@Service("wardService")
public class WardServiceImpl implements WardService {
    @Resource
    private WardMapper wardMapper;

    @Autowired
    private BedService bedService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Ward queryById(Integer id) {
        return this.wardMapper.queryById(id);
    }


    /**
     * 查询所有通过实体类属性
     *
     * @param pageParam
     * @return
     */
    @Override
    public PageInfo<Ward> queryAll(PageParam<Ward> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<Ward> wardList = wardMapper.queryWardAllAndBed(pageParam.getModel());
        System.out.println(wardList);
        PageInfo<Ward> pageInfo = new PageInfo<>(wardList);

        return pageInfo;
    }

    /**
     * 新增数据
     *
     * @param ward 实例对象
     * @return 实例对象
     */
    @Override
    public Ward insert(Ward ward) {
        this.wardMapper.insert(ward);
        return ward;
    }

    /**
     * 修改数据
     *
     * @param ward 实例对象
     * @return 实例对象
     */
    @Override
    public Ward update(Ward ward) {
        if (ward.getWardStatus().equals("0")) {
            List<Bed> bedList = bedService.queryByWardId(ward.getId().toString());
            for (Bed bed : bedList) {
                bed.setBedStatus("0");
                bedService.update(bed);
            }
        } else if (ward.getWardStatus().equals("2")) {
            List<Bed> bedList = bedService.queryByWardId(ward.getId().toString());
            for (Bed bed : bedList) {
                bed.setBedStatus("2");
                bedService.update(bed);
            }
        }
        if (ward.getWardPrice() != null & (ward.getWardPrice() == 80 || ward.getWardPrice() == 120 || ward.getWardPrice() == 300)) {
            if (ward.getWardType().equals("0")) {
                ward.setWardPrice(80.00);
            } else if (ward.getWardType().equals("1")) {
                ward.setWardPrice(120.00);
            } else {
                ward.setWardPrice(300.00);
            }
        }
        //删除病房的同时，修改其下所有病床状态
        if (ward.getIsDelete() == 1) {
            List<Bed> bedList = bedService.queryByWardId(ward.getId().toString());
            for (Bed bed : bedList) {
                bed.setIsDelete(1);
                bedService.update(bed);
            }
        }


        this.wardMapper.update(ward);
        return this.queryById(ward.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.wardMapper.deleteById(id) > 0;
    }

    @Override
    public List<Ward> selectAllByDepId(String depId) {
        Ward ward = new Ward();
        ward.setDepId(Integer.parseInt(depId));
        return wardMapper.queryAll(ward);
    }
}
